package com.fiap.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.fiap.event.GPSEvent;
import com.fiap.service.exception.GPSServiceException;
import com.google.android.gms.maps.model.LatLng;

/**
 * @author Felipe C. Tomazzi
 * Created 10/08/2013
 *
 */
public class Corrida {
	 
	private static Corrida instance = new Corrida();
    private boolean resumed;
    private boolean inForeground;
    private boolean running;
    private long startElapsedTime;
   
    private ArrayList<LatLng> arrayPositionsRoute;
	
	private Corrida() {
		arrayPositionsRoute = new ArrayList<LatLng>();
	}
	
	public static Corrida getInstance () {
		return instance;
	}
	
	public void onActivityCreated(){
		/*
		 * To be implemented 
		 */
		
	}
	
	public void onActivityStarted() {
        if (!inForeground) {
            /* 
             * Started activities should be visible (though not always interact-able),
             * so you should be in the foreground here.
             *
             * Register your location listener here. 
             */
            inForeground = true;            
            
        }
    }

    public void onActivityResumed() {
        resumed = true;
    }

    public void onActivityPaused() {
        resumed = false;
    }

    public void onActivityStopped() {
        if (!resumed) {
            /* If another one of your activities had taken the foreground, it would
             * have tripped this flag in onActivityResumed(). Since that is not the
             * case, your app is in the background.
             *
             * Unregister your location listener here.
             */
            inForeground = false;
        }
    }
    
    public void onActivityDestroy(){
    	/*
    	 * Destroy the Corrida obj.
    	 */
    	this.stop();
    	instance = null;
    }
    
    public ArrayList<LatLng> getArrayPositionsRoute(){
    	return arrayPositionsRoute;
    }
    
    public void addPosition(LatLng position){
    	arrayPositionsRoute.add(position);
    }
    
    public void start(){
    	if (!isRunning()){
    		running = true;
    	}
    }
    
    public void stop(){
    	this.setStartElapsedTime(0);
    	running = false;
    	arrayPositionsRoute.clear();
    }    
    
    public boolean isRunning(){
    	return running;
    }
    
    public void setStartElapsedTime(long elapsedTime){
    	if (this.startElapsedTime == 0){
    		this.startElapsedTime = elapsedTime;	
    	}
    }
    
    public long getStartElapsedTime(){
    	return startElapsedTime;
    }
	
}
